package controllers

import java.io.File

import dao._
import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.routing.JavaScriptReverseRouter
import play.api.libs.json.Json
import tool._

import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by Administrator on 2019/8/7
 */
class AppController @Inject()(cc: ControllerComponents, formTool: FormTool, accountDao: AccountDao, userDao: UserDao) extends
  AbstractController(cc) {

  def loginBefore = Action { implicit request =>
    Ok(views.html.login1())
  }

  def login = Action.async { implicit request =>
    val data = formTool.loginUserForm.bindFromRequest().get
    accountDao.selectById1.zip(userDao.select(data.name, data.password)).map { case (account, optionUser) =>
      if (data.name == account.account && data.password == account.password) {
        Redirect(routes.AdminController.userManageBefore()).addingToSession("admin" -> data.name,
          "errorNum" -> s"0")
      } else if (optionUser.isDefined) {
        val user = optionUser.get
        Redirect(routes.UserController.missionManageBefore()).addingToSession("user" -> data.name,
          "id" -> user.id.toString,"errorNum" -> s"0")
      } else {
        val errorNum = request.session.get("errorNum").getOrElse("0").toInt + 1
        Redirect(routes.AppController.loginBefore()).flashing("info" -> "用户名或密码错误!").addingToSession(
          "errorNum" -> s"${errorNum}"
        )
      }
    }
  }

  def javascriptRoutes = Action { implicit request =>
    Ok(
      JavaScriptReverseRouter("jsRoutes")(
        controllers.routes.javascript.AdminController.getAllUser,
        controllers.routes.javascript.AdminController.userNameCheck,
        controllers.routes.javascript.AdminController.deleteUserById,
        controllers.routes.javascript.AdminController.addUser,
        controllers.routes.javascript.AdminController.getUserById,
        controllers.routes.javascript.AdminController.updateUser,
        controllers.routes.javascript.AdminController.getTreeNodes,
        controllers.routes.javascript.AdminController.updateGzPath,
        controllers.routes.javascript.AdminController.gzPathCheck,
        controllers.routes.javascript.AdminController.getGzPath,
        controllers.routes.javascript.AdminController.getChildrenNodes,
        controllers.routes.javascript.AdminController.updatePosStrSite,
        controllers.routes.javascript.AdminController.updatePosSnpSite,
        controllers.routes.javascript.AdminController.getPosSiteInfo,
        controllers.routes.javascript.AdminController.getFiles,

        controllers.routes.javascript.MissionController.missionNameCheck,
        controllers.routes.javascript.MissionController.newMission,
        controllers.routes.javascript.MissionController.getAllMission,
        controllers.routes.javascript.MissionController.downloadData,
        controllers.routes.javascript.MissionController.deleteMissionById,
        controllers.routes.javascript.MissionController.updateMissionSocket,
        controllers.routes.javascript.MissionController.getLogContent,
        controllers.routes.javascript.MissionController.newBatchMission,
        controllers.routes.javascript.MissionController.getTreeNodes,
        controllers.routes.javascript.MissionController.missionSummaryBefore,
        controllers.routes.javascript.MissionController.getMissionById,
        controllers.routes.javascript.MissionController.getSampleStatInfo,
        controllers.routes.javascript.MissionController.getPosSamples,
        controllers.routes.javascript.MissionController.getNegSamples,
        controllers.routes.javascript.MissionController.missionSummaryBeforeByName,
        controllers.routes.javascript.MissionController.getNormalSampleInfo,
        controllers.routes.javascript.MissionController.search,
        controllers.routes.javascript.MissionController.getFiles,
        controllers.routes.javascript.MissionController.getChildrenNodes,

        controllers.routes.javascript.ToolController.getChildrenNodes,

        controllers.routes.javascript.SampleController.sampleManageBefore,
        controllers.routes.javascript.SampleController.getAllSample,
        controllers.routes.javascript.SampleController.deleteSampleById,
        controllers.routes.javascript.SampleController.getReadsData,
        controllers.routes.javascript.SampleController.getSeqData,
        controllers.routes.javascript.SampleController.getBasicData,
        controllers.routes.javascript.SampleController.getStatData,
        controllers.routes.javascript.SampleController.getSnpReadsData,
        controllers.routes.javascript.SampleController.getAllSampleByMissionId,
        controllers.routes.javascript.SampleController.downloadReport,
        controllers.routes.javascript.SampleController.exportReports,
        controllers.routes.javascript.SampleController.search,
        controllers.routes.javascript.SampleController.sampleSummaryBefore,
        controllers.routes.javascript.SampleController.getSampleById,
        controllers.routes.javascript.SampleController.newCompareMission,
        controllers.routes.javascript.SampleController.missionNameCheck,
        controllers.routes.javascript.SampleController.compareMissionDetailBefore,
        controllers.routes.javascript.SampleController.getMissionInfoById,
        controllers.routes.javascript.SampleController.getCompareReadsData,
        controllers.routes.javascript.SampleController.getCompareSnpReadsData,
        controllers.routes.javascript.SampleController.getCompareVennData,
        controllers.routes.javascript.SampleController.getCompareDiffReadsData,
        controllers.routes.javascript.SampleController.getCompareRawSeqData,
        controllers.routes.javascript.SampleController.getCompareDiffSnpReadsData,
        controllers.routes.javascript.SampleController.getCompareRawSnpSeqData,
        controllers.routes.javascript.SampleController.deletes,
        controllers.routes.javascript.SampleController.getAllSampleByServer,
        controllers.routes.javascript.SampleController.getAllSampleIds,
        controllers.routes.javascript.SampleController.refreshSelectColumns,
        controllers.routes.javascript.SampleController.getSelectColumns,
        controllers.routes.javascript.SampleController.exportDta,
        controllers.routes.javascript.SampleController.getSampleInfo,
        controllers.routes.javascript.SampleController.getAllMissionName,
        controllers.routes.javascript.SampleController.getAllContainerName,
        controllers.routes.javascript.SampleController.getCurrentTime,
        controllers.routes.javascript.SampleController.exportAllDat,
        controllers.routes.javascript.SampleController.deleteSample,
        controllers.routes.javascript.SampleController.getAllId,

        controllers.routes.javascript.StepMissionController.newStepMission,
        controllers.routes.javascript.StepMissionController.missionManageBefore,
        controllers.routes.javascript.StepMissionController.missionNameCheck,
        controllers.routes.javascript.StepMissionController.getAllMission,
        controllers.routes.javascript.StepMissionController.updateMissionSocket,
        controllers.routes.javascript.StepMissionController.deleteMissionById,
        controllers.routes.javascript.StepMissionController.getLogContent,
        controllers.routes.javascript.StepMissionController.missionResultBefore,
        controllers.routes.javascript.StepMissionController.getMissionResultData,
        controllers.routes.javascript.StepMissionController.getMissionById,
        controllers.routes.javascript.StepMissionController.checkSampleExist,
        controllers.routes.javascript.StepMissionController.exportAllDat,

        controllers.routes.javascript.DatController.xml2Dat,


        controllers.routes.javascript.UserController.missionManageBefore,

      )
    ).as("text/javascript")

  }


}
